package com.ruibang.glass.material.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.material.constant.Constants;
import com.ruibang.glass.material.constant.MaterialWareHouseCode;
import com.ruibang.glass.material.domain.req.WareHouseInfoReq;
import com.ruibang.glass.material.domain.resp.GlassContainerWarehouseInfo;
import com.ruibang.glass.material.dto.SelectDTO;
import com.ruibang.glass.material.dto.WareHouseInfoDTO;
import com.ruibang.glass.material.entity.MaterialWarehouseInfo;
import com.ruibang.glass.material.mapper.GMaterialWarehouseInfoMapper;
import com.ruibang.glass.material.service.MaterialWarehouseInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 物管-仓库管理-仓库信息管理  服务实现类
 * </p>
 *
 * @author mameng
 * @since 2024-03-30
 */
@Service
public class MaterialWarehouseInfoServiceImpl extends ServiceImpl<GMaterialWarehouseInfoMapper, MaterialWarehouseInfo> implements MaterialWarehouseInfoService {
    /**
     * 列表页查询
     *
     * @param warehouseInfo
     * @return
     */
    @Override
    public IPage<MaterialWarehouseInfo> getPage(WareHouseInfoReq warehouseInfo) {
        LambdaQueryWrapper<MaterialWarehouseInfo> queryWrapper = Wrappers.<MaterialWarehouseInfo>lambdaQuery();

        if (StringUtils.isNotBlank(warehouseInfo.getWarehouseName())) {
            queryWrapper.like(MaterialWarehouseInfo::getWarehouseName, warehouseInfo.getWarehouseName());
        }
        if (StringUtils.isNotBlank(warehouseInfo.getWarehouseNumber())) {
            queryWrapper.like(MaterialWarehouseInfo::getWarehouseNumber, warehouseInfo.getWarehouseNumber());
        }
        if (StringUtils.isNotBlank(warehouseInfo.getType())) {
            queryWrapper.eq(MaterialWarehouseInfo::getWarehouseCode, warehouseInfo.getType());
        }
        if (StringUtils.isNotBlank(warehouseInfo.getStatus())) {
            queryWrapper.eq(MaterialWarehouseInfo::getStatus, warehouseInfo.getStatus());
        }

        Page<MaterialWarehouseInfo> materialWarehouseInfoPage = baseMapper.selectPage(new Page<>(warehouseInfo.getPageNo(), warehouseInfo.getPageSize()),
                queryWrapper
        );
        //类型转换
        for (MaterialWarehouseInfo materialWarehouseInfo : materialWarehouseInfoPage.getRecords()) {
            for (MaterialWareHouseCode ele : MaterialWareHouseCode.values()) {
                if(StringUtils.isNotBlank(materialWarehouseInfo.getWarehouseCode())){
                    if (materialWarehouseInfo.getWarehouseCode().equals(ele.getMsg())){
                        materialWarehouseInfo.setType(ele.getCode());
                    }
                }
            }
        }
        return materialWarehouseInfoPage;
    }

    /**
     * 新增
     *
     * @param
     * @return
     */

    @Override
    public void add(MaterialWarehouseInfo materialWarehouseInfo) {
        baseMapper.insert(materialWarehouseInfo);
    }

    /**
     * 状态跟新
     *
     * @param id
     */
    @Override
    public void updateStatus(String id) {
        MaterialWarehouseInfo materialWarehouseInfo = baseMapper.selectById(id);
        if (materialWarehouseInfo.getStatus().equals(Constants.ONE_STR)) {
            materialWarehouseInfo.setStatus(Constants.TWO_STR);
            baseMapper.updateById(materialWarehouseInfo);
            return;
        }
        if (materialWarehouseInfo.getStatus().equals(Constants.TWO_STR)) {
            materialWarehouseInfo.setStatus(Constants.ONE_STR);
            baseMapper.updateById(materialWarehouseInfo);
        }

    }

    /**
     * 仓库下拉
     *
     * @param
     */
    @Override
    public List<MaterialWarehouseInfo> getWareList() {
        List<MaterialWarehouseInfo> materialWarehouseInfos = baseMapper.selectList(Wrappers.<MaterialWarehouseInfo>lambdaQuery()
                .select(MaterialWarehouseInfo::getWarehouseName, MaterialWarehouseInfo::getId, MaterialWarehouseInfo::getWarehouseCode)
                .isNotNull(MaterialWarehouseInfo::getWarehouseName));
        return materialWarehouseInfos;
    }

    @Override
    public List<MaterialWarehouseInfo> getFactorySelectList() {
        List<MaterialWarehouseInfo> materialWarehouseInfos = baseMapper.selectList(Wrappers.<MaterialWarehouseInfo>lambdaQuery().select(MaterialWarehouseInfo::getBelongToFactory));
        for (MaterialWarehouseInfo materialWarehouseInfo : materialWarehouseInfos) {
            materialWarehouseInfo.setId(materialWarehouseInfo.getBelongToFactory());
        }
        return materialWarehouseInfos;
    }

    /**
     * 仓库下拉
     *
     * @param
     */
    @Override
    public GlassContainerWarehouseInfo getWarehouseInfoByWarehousePositionCode(String warehousePositionCode) {
        if (StringUtils.isNotBlank(warehousePositionCode)) {
            return baseMapper.getWarehouseInfoByWarehousePositionCode(warehousePositionCode);
        }
        return null;
    }

    @Override
    public List<MaterialWarehouseInfo> getWareHouseList(WareHouseInfoReq wareHouseInfoReq) {
        QueryWrapper<MaterialWarehouseInfo> qw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(wareHouseInfoReq.getWarehouseName())) {
            qw.lambda().like(MaterialWarehouseInfo::getWarehouseName, wareHouseInfoReq.getWarehouseName());
        }
        return this.list(qw);
    }

}
